package com.ailk.kbi.kbiwarden.wsi.util

import java.util.ArrayList
import java.util.UUID

import com.ailk.kbi.kbiwarden.wsi.type.request.AuthInfoType;
import com.ailk.kbi.kbiwarden.wsi.vo.QueryVO;
import com.ailk.kbi.kbiwarden.wsi.vo.SessionVO;

class SessionHandler {
	static List<SessionVO> sessions = new ArrayList<SessionVO>()
	
	static SessionVO newSession(AuthInfoType login, QueryVO queryVO, String notifyAddress){
		SessionVO sessionVO = new SessionVO()
		String uuid = UUID.randomUUID().toString()
		sessionVO.userName = login.userName
		sessionVO.sessionId = uuid
		sessionVO.queryVO = queryVO
		sessionVO.notifyAddress = notifyAddress
		sessions.add(sessionVO)
		sessionVO
	}
	
	static SessionVO getSession(String sessionId){
		SessionVO session = sessions.find { it.sessionId == sessionId}
		if(session == null){
			throw new Exception("会话[" + sessionId + "]已结束！")
		}
		return session
	}
	
	static void deleteSession(String sessionId){
		SessionVO session = getSession(sessionId)
		sessions.remove(session)
		session = null
	}
}
